package club.cearnach.web.controller.admin;

import club.cearnach.web.controller.BaseController;
import club.cearnach.entity.Admin;
import club.cearnach.entity.Role;
import club.cearnach.api.exception.AdminException;
import club.cearnach.api.exception.RoleNotFoundException;
import club.cearnach.api.form.PageBean;
import club.cearnach.api.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;

/**
 * @author 阮胜
 * @date 2018/3/27 21:00
 */
@Api(tags = "后台管理员控制类")
@Controller
@RequestMapping("/admin")
@Slf4j
public class BackgroundAdminController extends BaseController {
    private static final String ADMIN_LIST = "admin/admin-list";

    private final IRoleService roleService;

    public BackgroundAdminController(IActorService actorService, IAdminService adminService, IDirectorService directorService, IMovieLanguageService movieLanguageService, IMovieService movieService, IMovieTypeService movieTypeService, IPersonService personService, IPlaceService placeService, IVisitorService visitorService, IRoleService roleService) {
        super(actorService, adminService, directorService, movieLanguageService, movieService, movieTypeService, personService, placeService, visitorService);
        this.roleService = roleService;
    }

    @GetMapping("/login/*")
    public void loginInvalid() {
    }

    @ApiOperation("后台管理员登录页面")
    @GetMapping("/login/form")
    public String loginForm() {
        return "admin/login/index";
    }


    @ApiOperation("保存管理员")
    @PostMapping(value = "/save")
    @ResponseBody
    public void save(Admin admin, @Min(1) Integer role) throws RoleNotFoundException {
        Role r = roleService.findById(role).orElseThrow(RoleNotFoundException::new);
        admin.setRole(r);
        adminService.save(admin);
    }


    @ApiOperation("获取管理员列表")
    @GetMapping("/list")
    public String list(PageBean pageBean, Model model) {
        Page<Admin> adminPage = adminService.findAllOrderByCreateDate(pageBean);
        List<Admin> adminList = adminPage.getContent();
        model.addAttribute(adminList);
        model.addAttribute("currentPage", pageBean.getPage());
        model.addAttribute("totalPage", adminPage.getTotalPages());
        return ADMIN_LIST;
    }


    @ApiOperation("删除管理员")
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable("id") @Min(0) int id) throws AdminException {
        adminService.delete(id);
        return true;
    }


    @ApiOperation("转发到管理员编辑页面")
    @GetMapping("/edit/form/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) throws AdminException {
        if (id != 0) {
            Admin admin = adminService.find(id);
            model.addAttribute(admin);
        }
        model.addAttribute(roleService.findAll());
        return "admin/admin-save";
    }


    @ApiOperation("批量删除")
    @DeleteMapping("/delete/all")
    @ResponseBody
    public void deleteAll(@RequestParam("id") Integer[] idArr) {
        adminService.deleteIn(idArr);
    }

    @ApiOperation("查重")
    @GetMapping("/exist")
    @ResponseBody
    public boolean isExist(String account, Integer id) {
        return adminService.isExist(account, id);
    }

}
